package com.jframe.sys.controller.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jframe.sso.model.CurUser;
import com.jframe.sys.controller.ExtendAction;
import com.jframe.sys.utils.CalendarUtil;
import com.jframe.sys.utils.FileUtil;
import com.jframe.sys.utils.JsonUtil;
import com.jframe.sys.utils.LogHandler;
import com.jframe.sys.utils.MacAddrUtil;
import com.jframe.sys.utils.StringUtil;
import com.jframe.sys.utils.Yz;


public class BaseAction extends SysService{
	
	protected LogHandler log;
	private PrintWriter out;
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected boolean printed=false;
	protected HttpSession session;
	protected CurUser curUser;
	protected PageInfo pageInfo;
	
	public CurUser getCurUser() {
		return curUser;
	}

	public void setCurUser(CurUser curUser) {
		this.curUser = curUser;
	}

	/**
	 * Servlet服务方法
	 * @throws ServletException
	 * @throws IOException
	 */
	public void service() throws ServletException, IOException{
		throw new ServletException("Please override service() Method!");
	}
	
	/**
	 * servlet初始化
	 * @param request
	 * @param response
	 */
	public void init(HttpServletRequest request,HttpServletResponse response) {
		this.setRequest(request);
		this.setResponse(response);
		this.session = this.request.getSession(true);	
		this.setCurUser((CurUser)this.session.getAttribute("curUser"));
		pageInfo = new PageInfo();
	}
	
	protected void println(Object text){
		print(text);
		println();
	}
	
	protected void println(){
		printed=true;
		getOut().println();
	}
	
	protected void print(Object text){
		printed=true;
		getOut().print(String.valueOf(text));
	}

	protected void flushOut(){
		getOut().flush();
	}
	protected void closeOut(){
		getOut().close();
	}
	
	/**
	 * 跳转到指定jsp或html页面
	 * @param url
	 */
	protected void forward(String url) {
		if(url.indexOf("?")!=-1){
			url +="&iframeId_="+this.pageInfo.getPageIframeId(request);			
		}else{
			url +="?iframeId_="+this.pageInfo.getPageIframeId(request);
		}
		url +="&tabId_="+this.pageInfo.getPageTabId(request);		
		try {
			request.getRequestDispatcher(url).forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	protected String getUrl(String url){
		if(url.indexOf("?")!=-1){
			url +="&iframeId_="+this.pageInfo.getPageIframeId(request);			
		}else{
			url +="?iframeId_="+this.pageInfo.getPageIframeId(request);
		}
		url +="&tabId_="+this.pageInfo.getPageTabId(request);
		url +="&datasrcId_="+this.pageInfo.getDataSrcId(request);
		return url;
	}
	
	public HttpServletRequest getRequest() {
		return request;
	}
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	public HttpServletResponse getResponse() {
		return response;
	}
	public void setContentType(String contextType) {
		response.setContentType(contextType);
	}
	
	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}
	
	public boolean isPrinted() {
		return printed;
	}

	public void dispose() {
		out=null;
		request=null;
		response=null;
		printed=false;
	}
	
	public String getIpAddr() {
		HttpServletRequest request = this.request;
		String ip = request.getHeader(" x-forwarded-for ");
		if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
			ip = request.getHeader(" Proxy-Client-IP ");
		}
		if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
			ip = request.getHeader(" WL-Proxy-Client-IP ");
		}
		if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	} 

	public PrintWriter getOut() {
		if (out==null){
			try {
				setOut(response.getWriter());
			} catch (IOException e) {
				//LogHandler.error(this,e);
			}
		}
		return out;
	}
	
	protected void sendRedirect(String url){
		try {
			this.response.sendRedirect(url);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void setOut(PrintWriter out) {
		this.out = out;
	}	
	
	public void getCombobox(){
		String key = StringUtil.null2Space(request.getParameter("key"));
		this.print(DicCache.getComboboxJson(key));
	}
	
	public void getCombotree(){
		String key = StringUtil.null2Space(request.getParameter("key"));
		this.print(TabmCache.getCombotreeJson(key));
	}
	
	/**
	 * valid token
	 * @return boolean 
	 */
	public boolean isTokenValid(){
		String token = request.getParameter(Token.TOKEN_NAME);
		return Token.isTokenValid(session, token);
	}
	
	
	
}
